package com.etc.test4.dao;



import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author MaZheng
 * @since 2020/12/9 - 9:58
 * database access object
 */
@Component
public class UserMastDao implements Dao {

    /**
     * 根据用户名查询用户信息数据
     *
     * @param uname 用户名
     * @return 用户信息数据
     */
    public void findByUname(String uname) {
        System.out.println("select **ue98af89sdaufa9s8f ");

    }

    public void insertuser(String uname, String psd) throws DaoException {
        {

            //jdbc链接数据库
            Connection conn = DBUtil.getConnection();
            try {
                conn.setAutoCommit(false);
                PreparedStatement ps = conn.prepareStatement("insert into user_mast values(null, ?, ?, ?, now(), ?)");
                ps.setString(2, psd);

                int r = ps.executeUpdate();
                if (r == 1) {
                    conn.commit();
                } else {
                    conn.rollback();
                    throw new DaoException("变更条数和你想的不一样");
                }

            } catch (SQLException throwables) {

                throwables.printStackTrace();
                try {
                    conn.rollback();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                throw new DaoException("你的SQL有问题", throwables);
            } finally {
                DBUtil.close();
            }
        }
    }
}
